﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Showroom.BLL;
using System.IO;
using ShowroomMobile.Controls;
using MyGeneration.dOOdads;

namespace ShowroomMobile
{
    public partial class ManagerProducts : AdminBasePage
    {
        private string _textSearch;
        private string TextSearch
        {
            get
            {
                return _textSearch;
            }
            set
            {
                _textSearch = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ((AdminSite)Page.Master).SetTitlePage("Products");
                gridSlides.Rebind();
            }
        }

        protected void gridSlides_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "DeleteAll")
            {
                //Delete all
                Showroom.BLL.Products.DeleteAllProducts();
                gridSlides.Rebind();
            }
            else if (e.CommandName == "Delete")
            {
                int ID = Convert.ToInt32(e.CommandArgument);
                Showroom.BLL.Products.DeleteProductByID(ID);
                gridSlides.Rebind();
            }
        }

        protected void gridSlides_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            gridSlides.CurrentPageIndex = e.NewPageIndex;
            gridSlides.Rebind();
        }

        protected void gridSlides_OnNeedDataSource(object sender, EventArgs e)
        {
            View_Products prods = new View_Products();
            string sFilter = string.Empty;
            if (!string.IsNullOrEmpty(TextSearch))
            {
                sFilter += string.Format("{0} LIKE '%{1}%'", View_Products.ColumnNames.Name, TextSearch);
            }
            int total = prods.Search(sFilter, "[ID] desc", gridSlides.CurrentPageIndex * gridSlides.PageSize, gridSlides.PageSize);
            gridSlides.VirtualItemCount = total;
            gridSlides.DataSource = prods.DefaultView;
        }

        protected void btnSearchProduct_Click(object sender, EventArgs e)
        {
            TextSearch = txtSearchProduct.Text.Trim();
            gridSlides.CurrentPageIndex = 0;
            gridSlides.Rebind();
        }
    }
}